package cui.xin.studentcourse.controller;

import com.google.code.kaptcha.Constants;
import cui.xin.studentcourse.bean.Student;
import cui.xin.studentcourse.bean.StudentSub;
import cui.xin.studentcourse.dto.ResponseBean;
import cui.xin.studentcourse.dto.StudentDto;
import cui.xin.studentcourse.service.CourseService;
import cui.xin.studentcourse.service.StudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@Slf4j
@RequestMapping(value = "/Student")
@Api(tags = "学生用户操作")
public class StudentController extends BaseController {

    @Autowired
    private StudentService studentService;

    @Autowired
    private CourseService courseService;

    //用户注册

    /**
     * 注册页面 用户名和密码和邮箱验证码参数
     * @param sub
     * @param session
     * @author palpitate_yzr
     * @date 2022年11月15日15:43:58
     * @return
     */
    @RequestMapping("/register")
    @ApiOperation(value = "注册",notes = "学生注册并验证邮箱")
    public ResponseBean<Void> studentRegister(@RequestBody StudentSub sub, HttpSession session) {

//        String email = student.getEmail();
//        //判断邮箱格式是否正确
//        if (Validator.isEmail(email)){
//            return new ResponseBean<>(6000,"邮箱格式错误");
//        }

        Student student = new Student();
        student.setStudentId(sub.getStudentId());
        student.setStudentName(sub.getStudentName());
        student.setStudentSex(sub.getStudentSex());
        student.setEmail(sub.getEmail());
        student.setPassword(sub.getPassword());
        String code = sub.getCode();
        System.out.println(code);

        //判断邮箱验证码是否正确
        String emailYzm = String.valueOf(session.getAttribute("emailYzm")) ;
        System.out.println(emailYzm);
        if (emailYzm.equals(code)){
            //执行插入操作
            int result = studentService.studentRegister(student);
            if (result==1){
                return new ResponseBean<>(304,"注册失败，用户名重复");
            }else if (result<1){
                return new ResponseBean<>(302,"服务器错误");
            }
            return new ResponseBean(result);
        }else{
           return new ResponseBean<>(300,"验证码错误");
        }



    }

    //用户登录
    @PostMapping("/login")
    @ApiOperation(value = "登录",notes = "学生用户登录")
    public ResponseBean<Student> studentLogin(@RequestBody StudentSub Sub,HttpSession session) {
        Student student = new Student();
        student.setStudentId(Sub.getStudentId());
        student.setPassword(Sub.getPassword());

        String code = Sub.getCode();
//        String code = Sub.getCode();
        //将存储在session的kaptcha所生成的验证码取出
        String validCode = (String) session.getAttribute(Constants.KAPTCHA_SESSION_KEY);
//        System.out.println(validCode);
        //判断验证码是否一致
        if (!validCode.equals(code)) {
            return new ResponseBean<>(300,"验证码错误");
        }
        //执行登录操作
        Student loginStudent = studentService.studentLogin(student);
        if (loginStudent==null){
            return new ResponseBean<>(303,"用户名或密码错误");
        }

        //分别将用户的session保存到服务端
        session.setAttribute("studentId", loginStudent.getStudentId());
        session.setAttribute("studentName", loginStudent.getStudentName());
        //优化一下传回前端的student数据，有些字段是不需要的。
        //只将用户名和Student_id进行回传
        Student newStudent = new Student();
        newStudent.setStudentName(loginStudent.getStudentName());
        newStudent.setStudentId(loginStudent.getStudentId());
        newStudent.setStudentSex(loginStudent.getStudentSex());

        return new ResponseBean<>(newStudent);
    }
    @GetMapping("/queryStudent")
    @ApiOperation(value = "查询个人信息",notes = "根据id查询学生个人信息")
    public ResponseBean<Student> queryStudentByStudentId(HttpSession session) {
        Long studentId = getStudentIdFromSession(session);
//        Long studentId = 1L;
        Student student = studentService.queryStudentByStudentId(studentId);
        //将id、用户名、性别、电话进行回传
        Student newStudent = new Student();
        newStudent.setStudentId(student.getStudentId());
        newStudent.setStudentName(student.getStudentName());
        newStudent.setStudentSex(student.getStudentSex());
        newStudent.setEmail(student.getEmail());
        newStudent.setPassword(student.getPassword());

        return new ResponseBean<>(newStudent);
    }
    /**
     *  忘记密码 验证邮箱然后重新设置
     *  首先要验证学号和邮箱是否存在 是否对应 然后 校验验证码 然后更改密码
     * @param sub
     * @author palpitate_yzr
     * @date  2022年11月15日16:09:11
     * @return
     */
    @PostMapping("/forgetPassword")
    @ApiOperation(value = "找回密码",notes = "忘记密码，验证邮箱后重新设置")
    public ResponseBean<Void> studentRestPassword(@RequestBody StudentSub sub,HttpSession session) {
        Student student = new Student();
        student.setStudentId(sub.getStudentId());
        student.setPassword(sub.getPassword());
        student.setEmail(sub.getEmail());
        String code = sub.getCode();
        //验证学号和邮箱
        int count= studentService.updatePassword(student);
        if (count<1){
            return new ResponseBean<>(301,"学号或邮箱数据错误");
        }
        //校验邮箱验证码
        String emailYzm = session.getAttribute("emailYzm").toString();
        if (!emailYzm.equals(code)){
            return new ResponseBean<>(300,"验证码错误");
        }
        //更改新密码
        int result = studentService.updatePassword(student);

        return new ResponseBean(result);
    }

    @PostMapping("/changePassword")
    @ApiOperation(value = "修改密码",notes = "个人信息页面修改密码，验证原密码正确后重新设置密码")
    public ResponseBean studentChangePassword(@RequestBody StudentSub sub, HttpSession session) {
        String newPassword = sub.getNewPassword();
        String oldPassword = sub.getOldPassword();
        Long studentId = getStudentIdFromSession(session);
        //更改新密码
        int result = studentService.changePassword(studentId,newPassword,oldPassword);
        if (result<1){
            return new ResponseBean<>(302,"服务器错误");
        }
        return new ResponseBean(result);
    }


    //处理用户退出登录的请求
    @GetMapping("/exit")
    @ApiOperation(value = "退出",notes = "账号登出")
    public ResponseBean<Void> exitStudentLoginStatus(HttpSession session) {
        session.removeAttribute("studentName");
        session.removeAttribute("studentId");
        return new ResponseBean<>();
    }
    /**
     * auth：yangbx
     * 已选课程展示
     * @param studentDto
     * @return
     */
    @GetMapping("/queryByConditiation")
    @ApiOperation(value = "展示课程",notes = "展示已选课程")
    public ResponseBean queryStudentByConditiation(@RequestBody(required = false) StudentDto studentDto,HttpSession session){
        System.out.println("====================================================");
        ResponseBean responseBean = new ResponseBean();
        Long studentId= (Long) session.getAttribute("studentId");
        List<StudentDto> result = studentService.queryListStudentByStudentId(studentId);
        responseBean.setContent(result);
        System.out.println(result);
        return responseBean;
    }

    //修改个人信息
    @PostMapping("/updateInfo")
    @ApiOperation(value = "修改",notes = "修改基本信息")
    public ResponseBean studentRestInfo(@RequestBody Student student) {
        System.out.println(student);
        int result = studentService.updateInfo(student);
        if (result<1){
            return new ResponseBean<>(302,"服务器错误");
        }
        return new ResponseBean(result);
    }

    @RequestMapping("/add")
    public void add(StudentDto studentDto){
        studentService.addStudent(studentDto);
    }

    @RequestMapping("/edit")
    public void edit(StudentDto studentDto){
        studentService.editStudent(studentDto);
    }

    @RequestMapping("/del/{id}")
    public void del(@PathVariable("id") Integer id){
        studentService.delStudentById(id);
    }

    @RequestMapping("/noStudentList")
    public List<StudentDto> selectNoCourseStuList() {
        return studentService.selectNoCourseStudentList();
    }
}
